package com.yanqu.road.server.manger.cross;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eBigRankType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eRankType;
import com.yanqu.road.entity.rank.UnionActivityRank;
import com.yanqu.road.entity.rank.UnionRank;
import com.yanqu.road.entity.rank.UserRank;
import com.yanqu.road.entity.rank.cross.CrossUnionActivityRank;
import com.yanqu.road.entity.servercenter.ServerGroup;
import com.yanqu.road.entity.union.cross.CrossUnionServerGroup;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.logic.pb.CrossActivityPb;
import com.yanqu.road.logic.pb.CrossRankPb;
import com.yanqu.road.logic.pb.RankPb;
import com.yanqu.road.logic.rank.RankListModel;
import com.yanqu.road.server.manger.CrossUnionServerGroupMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.pb.cross.group.CrossGroupProto;
import com.yanqu.road.pb.cross.tradewar.CrossTradeWarProto;
import com.yanqu.road.pb.decoration.DecorationProto;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.pb.rank.UnionActivityRankProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manger.ServerGroupMgr;
import com.yanqu.road.server.manger.activity.*;
import com.yanqu.road.server.manger.mammon.MammonMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.rank.ActivityRankMgr;
import com.yanqu.road.server.manger.rank.RankMgr;
import com.yanqu.road.server.manger.union.UnionActivityMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.date.DateHelper;

import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class CrossDataMgr extends TempMgr {

    private static HashSet<Integer> allSyncRankActivitySet;
 //   private static Map<Integer, RankProto.RankListMsg> crossActivityRankMsgMap;

    private static Map<Integer, Set<Integer>> allSyncRankActivityConditionTypeSet;
//    private static Map<Integer, Map<Integer, RankProto.RankListMsg>> crossActivityNewRankMsgMap;

//    private static Map<Integer, UnionActivityRankProto.UnionActivityRankListMsg> crossActivityUnionRankMsgMap;

    private static Map<Integer, CrossActivityProto.CrossActivityServerRankListMsg> activityServerRankMsgMap;

  //  private static Map<Integer, Map<Integer, RankProto.RankListMsg>> crossGroupRankMsgMap;

 //   private static RankProto.UnionRankListMsg crossGroupUnionRankMsg;

    private static Map<Integer, Map<Integer, Map<Long, UserRank>>> needUploadGroupRankMap = new ConcurrentHashMap<>();
    private static Map<Long, Map<Integer, UserRank>> allPatronsNeedUploadGroupRankMap = new ConcurrentHashMap<>();
    private static Map<Long, Map<Integer, UserRank>> allBeautyNeedUploadGroupRankMap = new ConcurrentHashMap<>();
    private static Map<Long, Map<Integer, UserRank>> allCurioNeedUploadGroupRankMap = new ConcurrentHashMap<>();
    private static Map<Integer, Map<Integer, Map<Long, UserRank>>> needUploadGroupRankToUnionCrossMap = new ConcurrentHashMap<>();
    private static Map<Integer, Map<Integer, Long>> uploadToUnionCrossTimeMap = new ConcurrentHashMap<>();

    /**
     * 跨服商会争霸
     */
    private static Map<Integer, CrossActivityProto.CrossActivityUnionRankListMsg> crossActivityUnionRankMsgMap2;

    private static Map<Integer, DecorationProto.MammonListMsg> crossGroupMammonMsgMap;

    private static Map<Integer, DecorationProto.MammonRecordListMsg> crossGroupMammonRecordMsgMap;

    private static Map<Integer,Map<Integer, CrossTradeWarProto.CrossTradeWarNoticeListMsg>> crossTradeWarNoticeListMsgMap;

    //activityId groupId conditionType
    private static Map<Integer, Map<Integer, Map<Integer, UnionActivityRankProto.UnionActivityRankListMsg>>> crossUnionConditionRankMap;

    private static Map<Integer, Map<Integer, Map<Long, UserRank>>> needUploadActivityRankMap = new ConcurrentHashMap<>();
    /**
     * 旧版冲榜活动
     */
    private static Map<Integer, Map<Long, UserRank>> uploadActivityRankCacheMap = new ConcurrentHashMap<>();
    private static Map<Integer, Map<Long, UserRank>> uploadActivityServerRankCacheMap = new ConcurrentHashMap<>();

    /**
     * 小当家类型活动定时同步榜单信息
     */
    private static Map<Integer, Map<Long, UserRank>> uploadRelationActivityRankTimerMap = new ConcurrentHashMap<>();


    public static void StrongholdRankChange(int activityId, int conditionType, UserRank userRank) {
        int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, userRank.getUserId());
        CrossActivityProto.CrossActivityUserRankChangeReqMsg.Builder reqMsg = CrossActivityProto.CrossActivityUserRankChangeReqMsg.newBuilder();
        RankProto.RankTempMsg.Builder msg = CrossRankPb.parseRankTempMsg(userRank);
        reqMsg.setActivityId(activityId);
        reqMsg.setConditionType(conditionType);
        reqMsg.addDataList(msg);
        reqMsg.setIsAll(false);
        GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C_CROSS_ACTIVITY_USER_RANK_CHANGE_NEW, reqMsg, 0, activityId, groupId);
    }

    @Deprecated
    public static void strongholdUnionRankChange(int activityId, int conditionType, UnionActivityRank unionRank) {
        if (unionRank.getValue().compareTo(BigInteger.ZERO) > 0) {
            int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, unionRank.getUnionUid());
            // 大于0才允许上榜
            CrossActivityProto.CrossActivityUnionRankChangeReqMsg.Builder req = CrossActivityProto.CrossActivityUnionRankChangeReqMsg.newBuilder();
            req.setActivityId(activityId);
            req.setConditionType(conditionType);
            req.setIsAll(false);
            UnionActivityRankProto.UnionActivityRankTempMsg.Builder msg = RankPb.parseUnionActivityRankTempMsg(unionRank);
            req.addDataList(msg);
            GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C_CROSS_ACTIVITY_UNION_RANK_CHANGE_NEW, req, 0, activityId, groupId);
        }
    }

    @Deprecated
    public static void generalActivityUserRankChange(int activityId, int conditionType, UserRank userRank) {
        CrossActivityProto.CrossActivityUserRankChangeReqMsg.Builder reqMsg = CrossActivityProto.CrossActivityUserRankChangeReqMsg.newBuilder();
        RankProto.RankTempMsg.Builder msg = CrossRankPb.parseRankTempMsg(userRank);
        reqMsg.setActivityId(activityId);
        reqMsg.setConditionType(conditionType);
        reqMsg.addDataList(msg);
        reqMsg.setIsAll(false);
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(CrossProtocol.C_CROSS_GENERAL_ACTIVITY_USER_RANK_CHANGE, reqMsg);
        GamePlayerMgr.sendPacket(0, pbMsg);
    }

    @Deprecated
    public static void generalActivityUnionRankChange(int activityId, int conditionType, UnionActivityRank unionRank, boolean isDel) {
        // 大于0才允许上榜
        CrossActivityProto.CrossActivityUnionRankChangeReqMsg.Builder req = CrossActivityProto.CrossActivityUnionRankChangeReqMsg.newBuilder();
        req.setActivityId(activityId);
        req.setConditionType(conditionType);
        req.setIsUnionDel(isDel);
        req.setIsAll(false);
        if (unionRank != null) {
            UnionActivityRankProto.UnionActivityRankTempMsg.Builder msg = RankPb.parseUnionActivityRankTempMsg(unionRank);
            req.addDataList(msg);
        }
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(CrossProtocol.C_CROSS_GENERAL_ACTIVITY_UNION_RANK_CHANGE, req);
        GamePlayerMgr.sendPacket(0, pbMsg);
    }

    /**
     * 群英争霸
     */
    private static Map<Integer, Map<Integer, Map<Integer, CrossActivityProto.UnionWarRankMsgMsg>>> unionWarRankMsgMap;

    public static void uploadGroupRank() {
        List<UserRank> userRankList = new ArrayList<>();
        int num = 0;
        boolean needBreak = false;
        synchronized (needUploadGroupRankMap) {
            Iterator<Map.Entry<Integer, Map<Integer, Map<Long, UserRank>>>> iterator = needUploadGroupRankMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<Integer, Map<Integer, Map<Long, UserRank>>> entry = iterator.next();
                Map<Integer, Map<Long, UserRank>> map = entry.getValue();
                if (map == null || map.isEmpty()) {
                    iterator.remove();
                    continue;
                }
                Iterator<Map.Entry<Integer, Map<Long, UserRank>>> entryIterator = map.entrySet().iterator();
                while (entryIterator.hasNext()) {
                    Map.Entry<Integer, Map<Long, UserRank>> mapEntry = entryIterator.next();
                    Map<Long, UserRank> rankMap = mapEntry.getValue();
                    if (rankMap == null || rankMap.isEmpty()) {
                        entryIterator.remove();
                        continue;
                    }
                    Iterator<Map.Entry<Long, UserRank>> iterator1 = rankMap.entrySet().iterator();
                    while (iterator1.hasNext()) {
                        Map.Entry<Long, UserRank> next = iterator1.next();
                        UserRank rank = next.getValue();
                        if (rank.getType() != eRankType.HistoryMaxAbility.getValue()) {
                            userRankList.add(rank);
                        }
                        iterator1.remove();
                        num++;
                        if (num >= 50) {
                            needBreak = true;
                            break;
                        }
                    }
                    if (needBreak) {
                        break;
                    }
                }
                if (needBreak) {
                    break;
                }
            }
           /* for (Map<Integer, Map<Long, UserRank>> map : needUploadGroupRankMap.values()) {
                for (Map<Long, UserRank> rankMap : map.values()) {
                    for (UserRank rank : rankMap.values()) {
                        userRankList.add(rank);
                        num++;
                        if (num >= 100) {
                            break;
                        }
                    }
                }
            }
            needUploadGroupRankMap.clear();*/
        }
        if (userRankList.size() > 0) {
            uploadGroupRank(userRankList);
        }
    }

    public static void uploadGroupRankToUnionCross() {
        List<UserRank> userRankListToUnionCross = new ArrayList<>();
        int num = 0;
        boolean needBreak = false;
        boolean isEmpty = false;
        synchronized (needUploadGroupRankToUnionCrossMap) {
            Iterator<Map.Entry<Integer, Map<Integer, Map<Long, UserRank>>>> iterator = needUploadGroupRankToUnionCrossMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<Integer, Map<Integer, Map<Long, UserRank>>> entry = iterator.next();
                Map<Integer, Map<Long, UserRank>> map = entry.getValue();
                if (map == null || map.isEmpty()) {
                    iterator.remove();
                    continue;
                }
                Iterator<Map.Entry<Integer, Map<Long, UserRank>>> entryIterator = map.entrySet().iterator();
                while (entryIterator.hasNext()) {
                    Map.Entry<Integer, Map<Long, UserRank>> mapEntry = entryIterator.next();
                    Map<Long, UserRank> rankMap = mapEntry.getValue();
                    if (rankMap == null || rankMap.isEmpty()) {
                        entryIterator.remove();
                        continue;
                    }
                    Iterator<Map.Entry<Long, UserRank>> iterator1 = rankMap.entrySet().iterator();
                    while (iterator1.hasNext()) {
                        Map.Entry<Long, UserRank> next = iterator1.next();
                        UserRank rank = next.getValue();
                        userRankListToUnionCross.add(rank);

                        iterator1.remove();
                        num++;
                        if (num >= 200) {
                            needBreak = true;
                            break;
                        }
                    }
                    if (rankMap.isEmpty()) {
                        entryIterator.remove();
                    }
                    if (needBreak) {
                        break;
                    }
                }
                if (map.isEmpty()) {
                    iterator.remove();
                }
                if (needBreak) {
                    break;
                }
            }
            isEmpty = needUploadGroupRankToUnionCrossMap.isEmpty();
        }

        if (!userRankListToUnionCross.isEmpty()) {
            uploadGroupRankToUnionCross(userRankListToUnionCross, isEmpty);
        }
    }

    public static boolean isNeedUploadGroupRankToUnionCrossEmpty(int bigType, int type) {
        if (!needUploadGroupRankToUnionCrossMap.containsKey(type)) {
            return true;
        }
        if (!needUploadGroupRankToUnionCrossMap.get(type).containsKey(bigType)) {
            return true;
        }
        return needUploadGroupRankToUnionCrossMap.get(type).get(bigType).isEmpty();
    }

    public static boolean checkUploadToUnionCrossTime(int bigType, int type) {
        long now = DateHelper.getCurrentTime();
        long uploadTime = getUnoloadToUnionCorssTime(bigType, type);
        if (now - uploadTime < DateHelper.SECOND_MILLIONS * 10) {
            return false;
        }
        return true;
    }


    public static void uploadAllPatronsGroupRank() {
        synchronized (allPatronsNeedUploadGroupRankMap) {
            uploadSpecifyGroupRank(allPatronsNeedUploadGroupRankMap);
        }
    }

    public static void uploadAllBeautyGroupRank() {
        synchronized (allBeautyNeedUploadGroupRankMap) {
            uploadSpecifyGroupRank(allBeautyNeedUploadGroupRankMap);
        }
    }

    public static void uploadAllCurioGroupRank() {
        synchronized (allCurioNeedUploadGroupRankMap) {
            uploadSpecifyGroupRank(allCurioNeedUploadGroupRankMap);
        }
    }

    private static void uploadSpecifyGroupRank(Map<Long, Map<Integer, UserRank>> rankMap) {
        List<UserRank> userRankList = new ArrayList<>();
        int num = 0;
        boolean needBreak = false;
        Iterator<Map.Entry<Long, Map<Integer, UserRank>>> iterator = rankMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Long, Map<Integer, UserRank>> entry = iterator.next();
            Map<Integer, UserRank> map = entry.getValue();
            if (map == null || map.isEmpty()) {
                iterator.remove();
                continue;
            }
            Iterator<Map.Entry<Integer, UserRank>> entryIterator = map.entrySet().iterator();
            while (entryIterator.hasNext()) {
                Map.Entry<Integer, UserRank> next = entryIterator.next();
                UserRank userRank = next.getValue();
                userRankList.add(userRank);
                entryIterator.remove();
                num++;
                if (num >= 50) {
                    needBreak = true;
                    break;
                }
            }
            if (needBreak) {
                break;
            }
        }
        if (userRankList.size() > 0) {
            uploadGroupRank(userRankList);
        }
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean reloadData() {
        /*crossActivityRankMsgMap = new ConcurrentHashMap<>();
        crossActivityNewRankMsgMap = new ConcurrentHashMap<>();
        crossActivityUnionRankMsgMap = new ConcurrentHashMap<>();
        crossGroupRankMsgMap = new ConcurrentHashMap<>();*/

        allSyncRankActivitySet = new HashSet<>();
        allSyncRankActivityConditionTypeSet = new ConcurrentHashMap<>();

        activityServerRankMsgMap = new ConcurrentHashMap<>();
        crossGroupMammonMsgMap = new ConcurrentHashMap<>();
        crossGroupMammonRecordMsgMap = new ConcurrentHashMap<>();
        crossTradeWarNoticeListMsgMap = new ConcurrentHashMap<>();
        crossActivityUnionRankMsgMap2 = new ConcurrentHashMap<>();
        crossUnionConditionRankMap = new ConcurrentHashMap<>();
        unionWarRankMsgMap = new ConcurrentHashMap<>();
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    public static void groupRankChange(UserRank userRank) {
        if (null == userRank) {
            return;
        }
        addNeedUploadGroupRank(userRank);
        if (overUploadSize()) {
            uploadGroupRank();
        }
    }

    public static void groupRankChangeToUnionCross(UserRank userRank) {
        if (null == userRank) {
            return;
        }
        addNeedUploadGroupToUnionCrossRank(userRank);
    }

    public static void groupAllPatronsRankChange(UserRank userRank) {
        if (null == userRank) {
            return;
        }
        addAllPatronsNeedUploadGroupRank(userRank);
    }

    public static void groupAllBeautyRankChange(UserRank userRank) {
        if (null == userRank) {
            return;
        }
        addAllBeautyNeedUploadGroupRank(userRank);
    }

    public static void groupAllCurioRankChange(UserRank userRank) {
        if (null == userRank) {
            return;
        }
        addAllCurioNeedUploadGroupRank(userRank);
    }



    private static boolean overUploadSize() {
       /* int num = 0;
        synchronized (needUploadGroupRankMap) {
            for (Map<Integer, Map<Long, UserRank>> map : needUploadGroupRankMap.values()) {
                for (Map<Long, UserRank> rankMap : map.values()) {
                    num = num + rankMap.size();
                }
            }
        }
        return num >= 100;*/
        return false;
    }

    private static void uploadGroupRank(List<UserRank> userRankList) {
        ServerGroup serverGroup = ServerGroupMgr.getServerGroupByServerId(GameServer.getInstance().getServerId());
        if (null == serverGroup || !serverGroup.isCrossRank()) {
            return;
        }
        CrossGroupProto.CrossGroupRankChangeReqMsg.Builder rankMsg = CrossGroupProto.CrossGroupRankChangeReqMsg.newBuilder();
        rankMsg.setGroupId(serverGroup.getGroupId());
        for (UserRank userRank : userRankList) {
            RankProto.RankTempMsg.Builder msg = CrossRankPb.parseRankTempMsg(userRank);
            if (null != msg) {
                CrossGroupProto.CrossGroupRankChangeMsg.Builder builder = CrossGroupProto.CrossGroupRankChangeMsg.newBuilder();
                builder.setBigRankType(userRank.getBigRankType());
                if(userRank.getBigRankType() == eBigRankType.CurioItem.getValue()){
                    msg.setLastUpdateTime(userRank.getLastUpdateTime());
                }
                builder.setData(msg);
                builder.setType(userRank.getType());
                rankMsg.addData(builder);
            }
        }
        YanQuMessage pbMessage = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_GROUP_RANK_CHANGE, rankMsg);
        GamePlayerMgr.sendPacket(0, pbMessage);
    }

    public static void uploadGroupRankToUnionCross(List<UserRank> userRankList, boolean isNeedUploadEmpty) {
        CrossUnionServerGroup serverGroup = CrossUnionServerGroupMgr.getServerGroupByServerId(GameServer.getInstance().getServerId());
        if (null == serverGroup || serverGroup.getState() == 0) {
            return;
        }
        long now = DateHelper.getCurrentTime();
        CrossGroupProto.CrossGroupRankChangeReqMsg.Builder rankMsg = CrossGroupProto.CrossGroupRankChangeReqMsg.newBuilder();
        rankMsg.setGroupId(serverGroup.getGroupId());
        for (UserRank userRank : userRankList) {
            RankProto.RankTempMsg.Builder msg = CrossRankPb.parseRankTempMsg(userRank);
            if (null != msg) {
                CrossGroupProto.CrossGroupRankChangeMsg.Builder builder = CrossGroupProto.CrossGroupRankChangeMsg.newBuilder();
                builder.setBigRankType(userRank.getBigRankType());
                builder.setData(msg);
                builder.setType(userRank.getType());
                rankMsg.addData(builder);

//                addUploadToUnionCrossTime(userRank.getBigRankType(), userRank.getType(), now);
            }
        }
        rankMsg.setIsEnd(isNeedUploadEmpty);

        YanQuMessage pbMessage = YanQuMessageUtils.buildMessage(Protocol.C_UNION_GROUP_USER_RANK_CHANGE, rankMsg);
        GamePlayerMgr.sendPacket(0, pbMessage);
    }

    private static void addNeedUploadGroupRank(UserRank userRank) {
        int type = userRank.getType();
        int bigRankType = userRank.getBigRankType();
        long userId = userRank.getUserId();
        synchronized (needUploadGroupRankMap) {
            Map<Long, UserRank> rankMap = getNeedGroupRank(type, bigRankType);
            rankMap.put(userId, userRank);
        }
    }

    private static void addNeedUploadGroupToUnionCrossRank(UserRank userRank) {
        int type = userRank.getType();
        int bigRankType = userRank.getBigRankType();
        long userId = userRank.getUserId();
        synchronized (needUploadGroupRankToUnionCrossMap) {
            Map<Long, UserRank> rankMap = getNeedGroupRankToUnionCross(type, bigRankType);
            rankMap.put(userId, userRank);
        }
    }

    public static void addUploadToUnionCrossTime(int bigRankType, int type, long time) {
        Map<Integer, Long> timeMap = uploadToUnionCrossTimeMap.get(type);
        if (null == timeMap) {
            synchronized (uploadToUnionCrossTimeMap) {
                timeMap = uploadToUnionCrossTimeMap.get(type);
                if (null == timeMap) {
                    timeMap = new ConcurrentHashMap<>();
                    uploadToUnionCrossTimeMap.put(type, timeMap);
                }
            }
        }
        timeMap.put(bigRankType, time);
    }

    public static long getUnoloadToUnionCorssTime(int bigRankType, int type) {
        Map<Integer, Long> timeMap = uploadToUnionCrossTimeMap.get(type);
        if (null != timeMap) {
            return timeMap.getOrDefault(bigRankType, 0l);
        }
        return 0l;
    }

    private static void addAllPatronsNeedUploadGroupRank(UserRank userRank) {
        long userId = userRank.getUserId();
        synchronized (allPatronsNeedUploadGroupRankMap) {
            Map<Integer, UserRank> rankMap = getAllPatronsNeedGroupRank(userId);
            rankMap.put(JSON.parseObject(userRank.getParam()).getIntValue("patronsId"), userRank);
        }
    }

    private static void addAllBeautyNeedUploadGroupRank(UserRank userRank) {
        long userId = userRank.getUserId();
        synchronized (allBeautyNeedUploadGroupRankMap) {
            Map<Integer, UserRank> rankMap = getAllBeautyNeedGroupRank(userId);
            rankMap.put(JSON.parseObject(userRank.getParam()).getIntValue("id"), userRank);
        }
    }

    private static void addAllCurioNeedUploadGroupRank(UserRank userRank) {
        long userId = userRank.getUserId();
        synchronized (allCurioNeedUploadGroupRankMap) {
            Map<Integer, UserRank> rankMap = getAllCurioNeedGroupRank(userId);
            rankMap.put(JSON.parseObject(userRank.getParam()).getIntValue("id"), userRank);
        }
    }


    private static  Map<Long, UserRank> getNeedGroupRank(int type, int bigRankType) {
        synchronized (needUploadGroupRankMap) {
            if (!needUploadGroupRankMap.containsKey(type)) {
                needUploadGroupRankMap.put(type, new ConcurrentHashMap<>());
            }
            Map<Integer, Map<Long, UserRank>> rankMap = needUploadGroupRankMap.get(type);
            if (!rankMap.containsKey(bigRankType)) {
                rankMap.put(bigRankType, new ConcurrentHashMap<>());
            }
            return rankMap.get(bigRankType);
        }
    }

    private static Map<Long, UserRank> getNeedGroupRankToUnionCross(int type, int bigRankType) {
        synchronized (needUploadGroupRankToUnionCrossMap) {
            if (!needUploadGroupRankToUnionCrossMap.containsKey(type)) {
                needUploadGroupRankToUnionCrossMap.put(type, new ConcurrentHashMap<>());
            }
            Map<Integer, Map<Long, UserRank>> rankMap = needUploadGroupRankToUnionCrossMap.get(type);
            if (!rankMap.containsKey(bigRankType)) {
                rankMap.put(bigRankType, new ConcurrentHashMap<>());
            }
            return rankMap.get(bigRankType);
        }
    }


    private static Map<Integer, UserRank> getAllPatronsNeedGroupRank(long userId) {
        if (!allPatronsNeedUploadGroupRankMap.containsKey(userId)) {
            synchronized (allPatronsNeedUploadGroupRankMap) {
                if (!allPatronsNeedUploadGroupRankMap.containsKey(userId)) {
                    allPatronsNeedUploadGroupRankMap.put(userId, new ConcurrentHashMap<>());
                }
            }
        }
        return allPatronsNeedUploadGroupRankMap.get(userId);
    }

    private static  Map<Integer, UserRank> getAllBeautyNeedGroupRank(long userId) {
        if (!allBeautyNeedUploadGroupRankMap.containsKey(userId)) {
            synchronized (allBeautyNeedUploadGroupRankMap) {
                if (!allBeautyNeedUploadGroupRankMap.containsKey(userId)) {
                    allBeautyNeedUploadGroupRankMap.put(userId, new ConcurrentHashMap<>());
                }
            }
        }
        return allBeautyNeedUploadGroupRankMap.get(userId);
    }

    private static  Map<Integer, UserRank> getAllCurioNeedGroupRank(long userId) {
        if (!allCurioNeedUploadGroupRankMap.containsKey(userId)) {
            synchronized (allCurioNeedUploadGroupRankMap) {
                if (!allCurioNeedUploadGroupRankMap.containsKey(userId)) {
                    allCurioNeedUploadGroupRankMap.put(userId, new ConcurrentHashMap<>());
                }
            }
        }
        return allCurioNeedUploadGroupRankMap.get(userId);
    }

    @Deprecated
    public static void groupUnionRankChange(UnionRank unionRank) {
//        if (null == unionRank) {
//            return;
//        }
//        ServerGroup serverGroup = ServerGroupMgr.getServerGroupByServerId(GameServer.getInstance().getServerId());
//        if (null == serverGroup || !serverGroup.isCrossRank()) {
//            return;
//        }
//        RankProto.UnionRankTempMsg.Builder msg = RankPb.parseUnionRankTempMsg(unionRank);
//        if (null != msg) {
//            CrossGroupProto.CrossGroupUnionRankChangeReqMsg.Builder rankMsg = CrossGroupProto.CrossGroupUnionRankChangeReqMsg.newBuilder();
//            rankMsg.setGroupId(serverGroup.getGroupId());
//            rankMsg.addDataList(msg);
////            YanQuMessage pbMessage = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_GROUP_UNION_RANK_CHANGE, rankMsg);
//            YanQuMessage pbMessage = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_SERVER_GROUP_UNION_RANK_CHANGE, rankMsg);
//            GamePlayerMgr.sendPacket(0, pbMessage);
//        }
    }

    /**
     * 变更数据到跨服
     */
//    public static void activityRankChange(int activityId, UserRank userRank, boolean isServerRank) {
//        RankProto.RankTempMsg.Builder msg = CrossRankPb.parseRankTempMsg(userRank);
//        CrossActivityProto.CrossActivityRankChangeReqMsg.Builder rankMsg = CrossActivityProto.CrossActivityRankChangeReqMsg.newBuilder();
//        rankMsg.setActivityId(activityId);
//        rankMsg.addDataList(msg);
//        rankMsg.setIsAll(false);
//        YanQuMessage pbMessage;
//        if (isServerRank) {
//            pbMessage = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_ACTIVITY_RANK_CHANGE, rankMsg);
//        } else {
//            pbMessage = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_ACTIVITY_USER_RANK_CHANGE, rankMsg);
//        }
//        GamePlayerMgr.sendChannelPacket(0, pbMessage);
//    }
    public static void activityRankChange(int activityId, UserRank userRank, boolean isServerRank) {
        Map<Integer, Map<Long, UserRank>> rankMap;
        if(isServerRank){
            rankMap = uploadActivityServerRankCacheMap;
        }else {
            rankMap = uploadActivityRankCacheMap;
        }
        synchronized (rankMap) {
            Map<Long, UserRank> userRankMap = rankMap.get(activityId);
            if (userRankMap == null) {
                userRankMap = new ConcurrentHashMap<>();
                rankMap.put(activityId, userRankMap);
            }
            userRankMap.put(userRank.getUserId(), userRank);
        }
    }
    public static void uploadActivityRankTask() {
        Map<Integer, Map<Long, UserRank>> tempMap = null;
        try {
            synchronized (uploadActivityRankCacheMap) {
                if (!uploadActivityRankCacheMap.isEmpty()) {
                    tempMap = new HashMap<>(uploadActivityRankCacheMap);
                    uploadActivityRankCacheMap.clear();
                }
            }
            if (null != tempMap && !tempMap.isEmpty()) {
                for (Map.Entry<Integer, Map<Long, UserRank>> entryData : tempMap.entrySet()) {
                    uploadActivityRankToCross(entryData.getKey(), false, entryData.getValue().values());
                }
            }
        }catch (Exception e){
            getLogger().error("uploadActivityRankTask userRank", e);
        }
        //区服榜单
        try {
            tempMap = null;
            synchronized (uploadActivityServerRankCacheMap) {
                if (!uploadActivityServerRankCacheMap.isEmpty()) {
                    tempMap = new HashMap<>(uploadActivityServerRankCacheMap);
                    uploadActivityServerRankCacheMap.clear();
                }
            }
            if (null != tempMap && !tempMap.isEmpty()) {
                for (Map.Entry<Integer, Map<Long, UserRank>> entryData : tempMap.entrySet()) {
                    uploadActivityRankToCross(entryData.getKey(), true, entryData.getValue().values());
                }
            }
        }catch (Exception e){
            getLogger().error("uploadActivityRankTask serverRank", e);
        }
    }
    private static void uploadActivityRankToCross(int activityId, boolean isServerRank, Collection<UserRank> userRankList) {
        if (userRankList == null || userRankList.isEmpty()) {
            return;
        }
        CrossActivityProto.CrossActivityRankChangeReqMsg.Builder rankMsg = CrossActivityProto.CrossActivityRankChangeReqMsg.newBuilder();
        rankMsg.setActivityId(activityId);
        for(UserRank userRank : userRankList){
            RankProto.RankTempMsg.Builder msg = CrossRankPb.parseRankTempMsg(userRank);
            if(null != msg) {
                rankMsg.addDataList(msg);
            }
        }
        rankMsg.setIsAll(false);
        YanQuMessage pbMessage;
        if (isServerRank) {
            pbMessage = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_ACTIVITY_RANK_CHANGE, rankMsg);
        } else {
            pbMessage = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_ACTIVITY_USER_RANK_CHANGE, rankMsg);
        }
        GamePlayerMgr.sendPacket(0, pbMessage);
    }

    /**
     * 变更数据到跨服（新冲榜活动）
     */
    public static void activityRankChange(int activityId, int conditionType, UserRank userRank) {
        synchronized (needUploadActivityRankMap) {
            Map<Integer, Map<Long, UserRank>> conditionMap = needUploadActivityRankMap.get(activityId);
            if (conditionMap == null) {
                conditionMap = new ConcurrentHashMap<>();
                needUploadActivityRankMap.put(activityId, conditionMap);
            }
            Map<Long, UserRank> userRankMap = conditionMap.get(conditionType);
            if (userRankMap == null) {
                userRankMap = new ConcurrentHashMap<>();
                conditionMap.put(conditionType, userRankMap);
            }
            userRankMap.put(userRank.getUserId(), userRank);
        }
    }

    public static void timeUploadActivityRank() {
        Map<Integer, Map<Integer, Map<Long, UserRank>>> tempMap;
        synchronized (needUploadActivityRankMap) {
            if (needUploadActivityRankMap.isEmpty()) {
                return;
            }
            tempMap = new HashMap<>(needUploadActivityRankMap);
            needUploadActivityRankMap.clear();
        }
        for (Map.Entry<Integer, Map<Integer, Map<Long, UserRank>>> entry : tempMap.entrySet()) {
            Integer activityId = entry.getKey();
            for (Map.Entry<Integer, Map<Long, UserRank>> mapEntry : entry.getValue().entrySet()) {
                Integer conditionType = mapEntry.getKey();
                uploadActivityRankToCross(activityId, conditionType, mapEntry.getValue().values());
            }
        }
    }

    private static void uploadActivityRankToCross(int activityId, int conditionType, Collection<UserRank> userRankList) {
        if (userRankList == null || userRankList.isEmpty()) {
            return;
        }
        CrossActivityProto.CrossActivityUserRankChangeReqMsg.Builder rankMsg = CrossActivityProto.CrossActivityUserRankChangeReqMsg.newBuilder();
        rankMsg.setActivityId(activityId);
        rankMsg.setConditionType(conditionType);
        rankMsg.setIsAll(false);
        for (UserRank userRank : userRankList) {
            RankProto.RankTempMsg.Builder msg = CrossRankPb.parseRankTempMsg(userRank);
            rankMsg.addDataList(msg);
        }
        YanQuMessage pbMessage = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_ACTIVITY_NEW_RANK_CHANGE, rankMsg);
        GamePlayerMgr.sendPacket(0, pbMessage);
    }

    /**
     * 商战排行变更数据到跨服
     */
    public static void tradeWarRankChange(int activityId, UserRank userRank) {
        RankProto.RankTempMsg.Builder msg = CrossRankPb.parseRankTempMsg(userRank);
        CrossActivityProto.CrossActivityRankChangeReqMsg.Builder rankMsg = CrossActivityProto.CrossActivityRankChangeReqMsg.newBuilder();
        rankMsg.setActivityId(activityId);
        rankMsg.addDataList(msg);
        rankMsg.setIsAll(false);
        YanQuMessage pbMessage = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_TRADE_WAR_RANK_CHANGE, rankMsg);
        GamePlayerMgr.sendPacket(0, pbMessage);
    }

    /**
     * 商战商会排行变更数据到跨服
     */
    public static void tradeWarUnionRankChange(int activityId, UnionActivityRank unionRank) {
        UnionActivityRankProto.UnionActivityRankTempMsg.Builder msg = RankPb.parseUnionActivityRankTempMsg(unionRank);
        CrossActivityProto.CrossActivityUnionRankChangeReqMsg.Builder rankMsg = CrossActivityProto.CrossActivityUnionRankChangeReqMsg.newBuilder();
        rankMsg.setActivityId(activityId);
        rankMsg.addDataList(msg);
        rankMsg.setIsAll(false);
        /*YanQuMessage pbMessage = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_TRADE_WAR_UNION_RANK_CHANGE, rankMsg);
        GamePlayerMgr.sendChannelPacket(0, pbMessage);*/
    }


    /**
     * 跨服盐场排行变更数据到跨服
     */
    public static void oreWarRankChange(int activityId, UserRank userRank) {
        RankProto.RankTempMsg.Builder msg = CrossRankPb.parseRankTempMsg(userRank);
        CrossActivityProto.CrossActivityRankChangeReqMsg.Builder rankMsg = CrossActivityProto.CrossActivityRankChangeReqMsg.newBuilder();
        rankMsg.setActivityId(activityId);
        rankMsg.addDataList(msg);
        rankMsg.setIsAll(false);
        YanQuMessage pbMessage = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_OREWAR_RANK_CHANGE, rankMsg);
        GamePlayerMgr.sendPacket(0, pbMessage);
    }

    /**
     * 跨服小當家類型排行变更数据到跨服
     */
    public static void relationRankChange(int activityId, UserRank userRank) {
        synchronized (uploadRelationActivityRankTimerMap) {
            Map<Long, UserRank> userRankMap = uploadRelationActivityRankTimerMap.get(activityId);
            if (userRankMap == null) {
                userRankMap = new ConcurrentHashMap<>();
                uploadRelationActivityRankTimerMap.put(activityId, userRankMap);
            }
            userRankMap.put(userRank.getUserId(), userRank);
        }
    }

    public static void uploadRelationActivityRankTask(){
        Map<Integer, Map<Long, UserRank>> tempMap = null;
        synchronized (uploadRelationActivityRankTimerMap) {
            if (!uploadRelationActivityRankTimerMap.isEmpty()) {
                tempMap = new HashMap<>(uploadRelationActivityRankTimerMap);
                uploadRelationActivityRankTimerMap.clear();
            }
        }
        if(null != tempMap){
            for(int activityId : tempMap.keySet()){
                for(UserRank userRank : tempMap.get(activityId).values()){
                    RankProto.RankTempMsg.Builder msg = CrossRankPb.parseRankTempMsg(userRank);
                    CrossActivityProto.CrossActivityRankChangeReqMsg.Builder rankMsg = CrossActivityProto.CrossActivityRankChangeReqMsg.newBuilder();
                    rankMsg.setActivityId(activityId);
                    rankMsg.addDataList(msg);
                    rankMsg.setIsAll(false);
                    YanQuMessage pbMessage = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_RELATION_ACTIVITY_RANK_CHANGE, rankMsg);
                    GamePlayerMgr.sendPacket(0, pbMessage);
                }
            }
            for(int activityId : tempMap.keySet()){
                BigInteger value = RelationActivityMgr.getUserActivityConditionValue(activityId, eGamePlayerEventType.CookBoy1IntegralServerCrossRank.getValue());
                UserRank rank = new UserRank();
                rank.setValue(value);
                //区服总的值也更新一下
                RankProto.RankTempMsg.Builder msg = CrossRankPb.parseRankTempMsg(rank);
                CrossActivityProto.CrossActivityRankChangeReqMsg.Builder rankMsg = CrossActivityProto.CrossActivityRankChangeReqMsg.newBuilder();
                rankMsg.setActivityId(activityId);
                rankMsg.addDataList(msg);
                rankMsg.setIsAll(false);
                YanQuMessage pbMessage = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_RELATION_ACTIVITY_SERVER_RANK_CHANGE, rankMsg);
                GamePlayerMgr.sendPacket(0, pbMessage);
                RelationActivityMgr.notifyServerValue(activityId,value);
            }
        }
    }


    /**
     * 跨服盐场商会排行变更数据到跨服
     */
    public static void oreWarUnionRankChange(int activityId, UnionActivityRank unionRank, boolean isUnionDel) {
        if (null == unionRank) {
            return;
        }
        UnionActivityRankProto.UnionActivityRankTempMsg.Builder msg = RankPb.parseUnionActivityRankTempMsg(unionRank);
        CrossActivityProto.CrossActivityUnionRankChangeReqMsg.Builder rankMsg = CrossActivityProto.CrossActivityUnionRankChangeReqMsg.newBuilder();
        rankMsg.setActivityId(activityId);
        rankMsg.addDataList(msg);
        rankMsg.setIsAll(false);
        rankMsg.setIsUnionDel(isUnionDel);
        YanQuMessage pbMessage = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_OREWAR_UNION_RANK_CHANGE, rankMsg);
        GamePlayerMgr.sendPacket(0, pbMessage);
    }
    /**
     * 跨服小当家类型商会排行变更数据到跨服
     */
    public static void relationUnionRankChange(int activityId, UnionActivityRank unionRank, boolean isUnionDel) {
        if (null == unionRank) {
            return;
        }
        UnionActivityRankProto.UnionActivityRankTempMsg.Builder msg = RankPb.parseUnionActivityRankTempMsg(unionRank);
        CrossActivityProto.CrossActivityUnionRankChangeReqMsg.Builder rankMsg = CrossActivityProto.CrossActivityUnionRankChangeReqMsg.newBuilder();
        rankMsg.setActivityId(activityId);
        rankMsg.addDataList(msg);
        rankMsg.setIsAll(false);
        rankMsg.setIsUnionDel(isUnionDel);
        YanQuMessage pbMessage = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_RELATION_ACTIVITY_UNION_RANK_CHANGE, rankMsg);
        GamePlayerMgr.sendPacket(0, pbMessage);
    }


    public static void unionActivityRandChange(int activityId, CrossUnionActivityRank activityRank, boolean isUnionDel) {
        CrossActivityProto.CrossActivityUnionRankTempMsg.Builder msg = CrossActivityPb.parseRankTempMsg(activityRank);
        CrossActivityProto.CrossActivityUnionRankChangeReqMsg2.Builder rankMsg = CrossActivityProto.CrossActivityUnionRankChangeReqMsg2.newBuilder();
        rankMsg.setActivityId(activityId);
        rankMsg.addDataList(msg);
        rankMsg.setIsAll(false);
        rankMsg.setIsUnionDel(isUnionDel);
       /* YanQuMessage pbMessage = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_UNION_ACTIVITY_RANK_CHANGE, rankMsg);
        GamePlayerMgr.sendChannelPacket(0, pbMessage);*/
    }

    public static void unionGeneralActivityRandChange(int activityId, CrossUnionActivityRank activityRank, boolean isUnionDel) {
        CrossActivityProto.CrossActivityUnionRankTempMsg.Builder msg = CrossActivityPb.parseRankTempMsg(activityRank);
        CrossActivityProto.CrossActivityUnionRankChangeReqMsg2.Builder rankMsg = CrossActivityProto.CrossActivityUnionRankChangeReqMsg2.newBuilder();
        rankMsg.setActivityId(activityId);
        rankMsg.addDataList(msg);
        rankMsg.setIsAll(false);
        rankMsg.setIsUnionDel(isUnionDel);
       /* YanQuMessage pbMessage = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_UNION_ACTIVITY_RANK_CHANGE, rankMsg);
        GamePlayerMgr.sendChannelPacket(0, pbMessage);*/
    }

    public static void unionActivityRandChange(int activityId, Map<String, UnionActivityConditionData> unionConditionDataMap, boolean isAll) {
        CrossActivityProto.CrossActivityUnionRankChangeReqMsg2.Builder rankMsg = CrossActivityProto.CrossActivityUnionRankChangeReqMsg2.newBuilder();
        rankMsg.setActivityId(activityId);
        rankMsg.setIsAll(isAll);
        rankMsg.setIsUnionDel(false);
        for (Map.Entry<String, UnionActivityConditionData> dataEntry : unionConditionDataMap.entrySet()) {
            CrossUnionActivityRank rank = new CrossUnionActivityRank();
            rank.setUnionBaseInfo(dataEntry.getValue().getUnionBaseInfo());
            rank.setUnionUid(dataEntry.getValue().getUnionUid());
            rank.setValue(dataEntry.getValue().getValue());
            rank.setActivityId(activityId);
            CrossActivityProto.CrossActivityUnionRankTempMsg.Builder msg = CrossActivityPb.parseRankTempMsg(rank);
            rankMsg.addDataList(msg);
        }
      /*  YanQuMessage pbMessage = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_UNION_ACTIVITY_RANK_CHANGE, rankMsg);
        GamePlayerMgr.sendChannelPacket(0, pbMessage);*/
    }

    /**
     * 跨服商会争霸数据变化
     * @param activityId  活动
     * @param unionConditionData 商会活动数据
     * @param isUnionDel  是否商会解散
     */
    public static void unionActivityRandChange(int activityId, UnionActivityConditionData unionConditionData, boolean isUnionDel) {
        CrossUnionActivityRank rank = new CrossUnionActivityRank();
        rank.setUnionUid(unionConditionData.getUnionUid());
        rank.setValue(unionConditionData.getValue());
        rank.setUnionBaseInfo(unionConditionData.getUnionBaseInfo());
        rank.setActivityId(activityId);
        unionActivityRandChange(activityId, rank, isUnionDel);
    }

    @Deprecated
    public static void unionGeneranlActivityRandChange(int activityId, UnionActivityConditionData unionConditionData, boolean isUnionDel) {
        UnionActivityRank rank = new UnionActivityRank();
        rank.setUnionUid(unionConditionData.getUnionUid());
        rank.setValue(unionConditionData.getValue());
        rank.setUnionBaseInfo(unionConditionData.getUnionBaseInfo());
        rank.setActivityId(activityId);
        generalActivityUnionRankChange(activityId, unionConditionData.getConditionType(), rank, isUnionDel);
    }

    /**
     * 活动结束，统一上传一下排行数据
     */
    public static void activityRankAllSync() {
        List<ActivityInfo> activityInfoList = NormalActivityMgr.getCrossRankActivityInfoList();
        for (ActivityInfo activityInfo : activityInfoList) {
            if (allSyncRankActivitySet.contains(activityInfo.getActivityId())) {//已经同步过了
                continue;
            }
            if (ActivityMgr.crossRankActivityCanAllSync(activityInfo)) {
                RankListModel rankListModel = RankMgr.getRankListModelByType(eBigRankType.Normal.getValue(), activityInfo.getActivityId());
                if (null != rankListModel) {
                    LinkedList<UserRank> rankList = rankListModel.getRankList();
                    if (rankList.size() > 0) {
                      /*  CrossActivityProto.CrossActivityRankChangeReqMsg.Builder rankMsg = parseCrossActivityRankChangeReqMsg(activityInfo.getActivityId(), rankList);
                        YanQuMessage pbMessage = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_ACTIVITY_RANK_CHANGE, rankMsg);
                        GamePlayerMgr.sendChannelPacket(0, pbMessage);*/
                    }
                }
                //上传跨服商会活动数据
                Map<String, UnionActivityConditionData> unionActivityConditionDataMap = UnionActivityMgr.getUnionActivityConditionDataMap(activityInfo.getActivityId());
                if (null != unionActivityConditionDataMap) {
                    CrossDataMgr.unionActivityRandChange(activityInfo.getActivityId(), unionActivityConditionDataMap, true);
                }
            }
        }
    }

    /**
     * 活动结束，统一上传一下排行数据
     */
    public static void activityNewRankAllSync() {
        List<ActivityInfo> activityInfoList = NormalActivityMgr.getNewRankActivityInfoList();
        for (ActivityInfo activityInfo : activityInfoList) {
            if (ActivityMgr.crossRankActivityCanAllSync(activityInfo)) {
                Map<Integer, RankListModel> rankListModelMap = ActivityRankMgr.getActivityRankListModelMap(activityInfo.getActivityId());
                if (null != rankListModelMap) {
                    for (Map.Entry<Integer, RankListModel> dataEntry : rankListModelMap.entrySet()) {
                        if (!ActivityHelper.conditionTypeIsCrossRank(dataEntry.getKey())) {
                            continue;
                        }
                        if(dataEntry.getKey() == eGamePlayerEventType.DaoMuPvpPersonRank.getValue()){
                            continue;
                        }
                        if(dataEntry.getKey() == eGamePlayerEventType.MusicRoomScoreRank.getValue()){
                            continue;
                        }
                        if(dataEntry.getKey() == eGamePlayerEventType.BowArtActivityScoreCrossRank.getValue()){
                            continue;
                        }
                        if (hasAllSyncActivityRankConditionType(activityInfo.getActivityId(), dataEntry.getKey())) {//还未全部上传
                            continue;
                        }

                        if (activityInfo.getType() == eActivityType.ZhenHuan.getValue()){
                            continue;
                        }

                        LinkedList<UserRank> rankList = dataEntry.getValue().getRankList();
                        if (rankList.size() > 0) {
                            CrossActivityProto.CrossActivityUserRankChangeReqMsg.Builder rankMsg = parseCrossActivityRankChangeReqMsg(
                                    activityInfo.getActivityId(), dataEntry.getKey(), rankList);
                            YanQuMessage pbMessage = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_ACTIVITY_NEW_RANK_CHANGE, rankMsg);
                            GamePlayerMgr.sendPacket(0, pbMessage);
                        }
                    }
                }
            }
        }
    }

    public static boolean hasAllSyncActivityRankConditionType(int activityId, int conditionType) {
        if (!allSyncRankActivityConditionTypeSet.containsKey(activityId)) {
            return false;
        }
        return allSyncRankActivityConditionTypeSet.get(activityId).contains(conditionType);
    }

    /**
     * 活动结束，统一上传一下排行数据
     */
    public static void commonActivityRankAllSync() {
        List<ActivityInfo> activityInfoList = CommonActivityMgr.getOpenCommonActivityInfoList(eActivityType.CrossCommonActivity.getValue());
        for (ActivityInfo activityInfo : activityInfoList) {
            if (allSyncRankActivitySet.contains(activityInfo.getActivityId())) {//已经同步过了
                continue;
            }
            if(ActivityMgr.crossRankActivityCanAllSync(activityInfo)){
                RankListModel rankListModel = CommonActivityRankMgr.getCrossRankListModel(activityInfo.getActivityId());
                if(null != rankListModel){
                    LinkedList<UserRank> rankList = rankListModel.getRankList();
                    if (rankList.size() > 0) {
                        CrossActivityProto.CrossActivityRankChangeReqMsg.Builder rankMsg = parseCrossActivityRankChangeReqMsg(activityInfo.getActivityId(), rankList);
                        YanQuMessage pbMessage = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_ACTIVITY_USER_RANK_CHANGE, rankMsg);
                        GamePlayerMgr.sendPacket(0, pbMessage);
                    }
                }
            }
        }
    }

    /**
     * 解析msg
     */
    private static CrossActivityProto.CrossActivityRankChangeReqMsg.Builder parseCrossActivityRankChangeReqMsg(int activityId, LinkedList<UserRank> rankList) {
        CrossActivityProto.CrossActivityRankChangeReqMsg.Builder rankMsg = CrossActivityProto.CrossActivityRankChangeReqMsg.newBuilder();
        rankMsg.setActivityId(activityId);
        rankMsg.setIsAll(true);
        for (UserRank userRank : rankList) {
            if (userRank.isDelete() || userRank.getUserId() == 0) {
                continue;
            }
            RankProto.RankTempMsg.Builder msg = CrossRankPb.parseRankTempMsg(userRank);
            if (null != msg) {
                rankMsg.addDataList(msg);
            }
        }
        return rankMsg;
    }

    /**
     * 解析msg，新活动
     */
    private static CrossActivityProto.CrossActivityUserRankChangeReqMsg.Builder parseCrossActivityRankChangeReqMsg(int activityId, int conditionType, LinkedList<UserRank> rankList) {
        CrossActivityProto.CrossActivityUserRankChangeReqMsg.Builder rankMsg = CrossActivityProto.CrossActivityUserRankChangeReqMsg.newBuilder();
        rankMsg.setActivityId(activityId);
        rankMsg.setConditionType(conditionType);
        rankMsg.setIsAll(true);
        for (UserRank userRank : rankList) {
            RankProto.RankTempMsg.Builder msg = CrossRankPb.parseRankTempMsg(userRank);
            if (null != msg) {
                rankMsg.addDataList(msg);
            }
        }
        return rankMsg;
    }

    /**
     * 增加活动完全同步标识
     */
    public static void addAllSyncRankActivity(int activityId) {
        allSyncRankActivitySet.add(activityId);
    }

    /**
     * 增加活动完全同步标识
     */
    public static void addAllSyncRankActivityConditionType(int activityId, int conditionType) {
        if (!allSyncRankActivityConditionTypeSet.containsKey(activityId)) {
            synchronized (allSyncRankActivityConditionTypeSet) {
                if (!allSyncRankActivityConditionTypeSet.containsKey(activityId)) {
                    allSyncRankActivityConditionTypeSet.put(activityId, new HashSet<>());
                }
            }
        }
        allSyncRankActivityConditionTypeSet.get(activityId).add(conditionType);
    }

    /**
     * 接收跨服同步的榜单
     * @param msg
     */
    public static void crossActivityNewRankListSync(CrossActivityProto.CrossActivityRankListMsg msg) {
        /*if (null != msg) {
            if (!crossActivityNewRankMsgMap.containsKey(msg.getActivityId())) {
                synchronized (crossActivityNewRankMsgMap) {
                    if (!crossActivityNewRankMsgMap.containsKey(msg.getActivityId())) {
                        crossActivityNewRankMsgMap.put(msg.getActivityId(), new ConcurrentHashMap<>());
                    }
                }
            }
            crossActivityNewRankMsgMap.get(msg.getActivityId()).put(msg.getConditionType(), msg.getRankList());
        }*/
    }

    public static void crossActivityRankListSync(CrossActivityProto.CrossActivityRankListMsg msg) {
      /*  if (null != msg) {
            crossActivityRankMsgMap.put(msg.getActivityId(), msg.getRankList());
        }*/
    }

    public static void crossActivityUnionRankListSync(CrossActivityProto.CrossActivityUnionRankListSyncMsg msg) {
      /*  if (null != msg) {
            crossActivityUnionRankMsgMap.put(msg.getActivityId(), msg.getRankList());
        }*/
    }

    public static void crossActivityServerRankListSync(CrossActivityProto.CrossActivityServerRankListSyncMsg msg) {
        if (null != msg) {
            activityServerRankMsgMap.put(msg.getActivityId(), msg.getServerRankList());
        }
    }

    public static void crossTradeWarNoticeListSync(CrossTradeWarProto.CrossTradeWarNoticeListSyncMsg msg) {
        if (null != msg) {
            if (!crossTradeWarNoticeListMsgMap.containsKey(msg.getActivityId())){
                synchronized (crossTradeWarNoticeListMsgMap){
                    if (!crossTradeWarNoticeListMsgMap.containsKey(msg.getActivityId())){
                        crossTradeWarNoticeListMsgMap.put(msg.getActivityId(),new ConcurrentHashMap<>());
                    }
                }
            }
            crossTradeWarNoticeListMsgMap.get(msg.getActivityId()).put(msg.getGroupId(),msg.getDataList());
            TradeWarActivityMgr.setIsFirstGetCrossNotice(true);
        }
    }

    //商会争霸同步跨服的数据通知
    public static void crossActivityUnionRankListSync(CrossActivityProto.CrossActivityUnionRankListSyncMsg2 msg) {
        if (null != msg) {
            int activityId = msg.getActivityId();
            crossActivityUnionRankMsgMap2.put(activityId, msg.getUnionRankList());
        }
    }

    //群英争霸跨服同步
    public static void unionWarRankListSync(CrossActivityProto.UnionWarRankMsgMsg msg) {
        if (msg != null) {
            int activityId = msg.getActivityId();
            if (!unionWarRankMsgMap.containsKey(activityId)) {
                unionWarRankMsgMap.put(activityId, new ConcurrentHashMap<>());
            }
            if (!unionWarRankMsgMap.get(activityId).containsKey(msg.getPhase())){
                unionWarRankMsgMap.get(activityId).put(msg.getPhase(), new ConcurrentHashMap<>());
            }
            unionWarRankMsgMap.get(activityId).get(msg.getPhase()).put(msg.getGroupId(), msg);
        }
    }

    //商会争霸
    public static CrossActivityProto.CrossActivityUnionRankListMsg.Builder getCrossActivityUnionRankListMsg(int activityId) {
        if (null != crossActivityUnionRankMsgMap2 && crossActivityUnionRankMsgMap2.containsKey(activityId)) {
            return crossActivityUnionRankMsgMap2.get(activityId).toBuilder();
        }
        return CrossActivityProto.CrossActivityUnionRankListMsg.newBuilder();
    }

    //群英争霸
    public static CrossActivityProto.UnionWarRankMsgMsg.Builder getUnionWarRankListMsg(int activityId, int phase, int groupId) {
        if (unionWarRankMsgMap != null) {
            if (unionWarRankMsgMap.containsKey(activityId)) {
                if (unionWarRankMsgMap.get(activityId).containsKey(phase)) {
                    if(unionWarRankMsgMap.get(activityId).get(phase).containsKey(groupId))
                    return unionWarRankMsgMap.get(activityId).get(phase).get(groupId).toBuilder();
                }
            }
        }
        return CrossActivityProto.UnionWarRankMsgMsg.newBuilder();
    }

    /**
     * 跨服联盟排行榜类型同步
     *
     * @param syncMsg
     */
    public static void crossActivityUnionConditionRankSync(CrossActivityProto.CrossActivityUnionRankListSyncMsg syncMsg) {
        int activityId = syncMsg.getActivityId();
        int conditionType = syncMsg.getConditionType();
        int groupId = syncMsg.getGroupId();
        UnionActivityRankProto.UnionActivityRankListMsg rankList = syncMsg.getRankList();
        Map<Integer, Map<Integer, UnionActivityRankProto.UnionActivityRankListMsg>> groupMap = crossUnionConditionRankMap.get(activityId);
        if (null == groupMap) {
            synchronized (crossUnionConditionRankMap) {
                groupMap = crossUnionConditionRankMap.get(activityId);
                if (null == groupMap) {
                    groupMap = new ConcurrentHashMap<>();
                    crossUnionConditionRankMap.put(activityId, groupMap);
                }
            }
        }
        Map<Integer, UnionActivityRankProto.UnionActivityRankListMsg> conditionMap = groupMap.get(groupId);
        if (null == conditionMap) {
            synchronized (crossUnionConditionRankMap) {
                conditionMap = groupMap.get(groupId);
                if (null == conditionMap) {
                    conditionMap = new ConcurrentHashMap<>();
                    groupMap.put(groupId, conditionMap);
                }
            }
        }

        conditionMap.put(conditionType, rankList);
    }

    public static UnionActivityRankProto.UnionActivityRankListMsg getCrossActivityUnionConditionTypeRankList(int activityId, int groupId, int conditionType) {
        Map<Integer, Map<Integer, UnionActivityRankProto.UnionActivityRankListMsg>> groupMap = crossUnionConditionRankMap.get(activityId);
        if (null != groupMap) {
            Map<Integer, UnionActivityRankProto.UnionActivityRankListMsg> conditionMap = groupMap.get(groupId);
            if (null != conditionMap) {
                return conditionMap.get(conditionType);
            }
        }
        return null;
    }

    /**
     * 获取跨服活动榜单集合
     * @param activityId
     * @return
     */
   /* public static Map<Integer, RankProto.RankListMsg> getCrossNewRankActivityRankMsgMap(int activityId) {
        return crossActivityNewRankMsgMap.get(activityId);
    }*/

    /**
     * 获取跨服榜单
     * @param activityId
     * @return
     */
 /*   public static RankProto.RankListMsg.Builder getCrossUserRankActivityRankMsg(int activityId, int conditionType) {
        Map<Integer, RankProto.RankListMsg> dataMap = getCrossNewRankActivityRankMsgMap(activityId);
        if (null != dataMap && dataMap.containsKey(conditionType)) {
            return dataMap.get(conditionType).toBuilder();
        }
        return RankProto.RankListMsg.newBuilder();
    }*/


  /*  public static RankProto.RankListMsg.Builder getCrossRankActivityRankMsg(int activityId) {
        if (null != crossActivityRankMsgMap && crossActivityRankMsgMap.containsKey(activityId)) {
            return crossActivityRankMsgMap.get(activityId).toBuilder();
        }
        return RankProto.RankListMsg.newBuilder();
    }*/

 /*   public static UnionActivityRankProto.UnionActivityRankListMsg.Builder getCrossRankActivityUnionRankMsg(int activityId) {
        if (null != crossActivityUnionRankMsgMap && crossActivityUnionRankMsgMap.containsKey(activityId)) {
            return crossActivityUnionRankMsgMap.get(activityId).toBuilder();
        }
        return UnionActivityRankProto.UnionActivityRankListMsg.newBuilder();
    }*/

    public static CrossActivityProto.CrossActivityServerRankListMsg.Builder getActivityServerRankMsg(int activityId) {
        if (null != activityServerRankMsgMap && activityServerRankMsgMap.containsKey(activityId)) {
            return activityServerRankMsgMap.get(activityId).toBuilder();
        }
        return CrossActivityProto.CrossActivityServerRankListMsg.newBuilder();
    }

    public static void crossGroupRankListSync(CrossGroupProto.CrossGroupRankListMsg msg) {
       /* if (null != msg) {
            if (!crossGroupRankMsgMap.containsKey(msg.getBigType())) {
                crossGroupRankMsgMap.put(msg.getBigType(), new ConcurrentHashMap<>());
            }
            crossGroupRankMsgMap.get(msg.getBigType()).put(msg.getSchoolType(), msg.getRankList());
        }*/
    }

    @Deprecated
    public static void crossGroupUnionRankListSync(RankProto.UnionRankListMsg msg) {
      /*  if (null != msg) {
            crossGroupUnionRankMsg = msg;
        }*/
    }

    public static void crossGroupMammonListSync(CrossGroupProto.CrossGroupMammonListMsg msg) {
        if (null != msg) {
            crossGroupMammonMsgMap.put(msg.getDecorationId(), msg.getDataList());
        }
    }

    public static void crossGroupMammonRecordListSync(CrossGroupProto.CrossGroupMammonRecordListMsg msg) {
        if (null != msg) {
            long lastRecordTime = 0;
            crossGroupMammonRecordMsgMap.put(msg.getDecorationId(), msg.getDataList());
            int length = msg.getDataList().getDataListCount();
            if (length > 0) {
                DecorationProto.MammonRecordTempMsg recordTempMsg = msg.getDataList().getDataList(length - 1);
                lastRecordTime = recordTempMsg.getRecordTime();
            }
            if (lastRecordTime > 0) {
                MammonMgr.notifyPlayerMammonCongratulate(msg.getDecorationId(), lastRecordTime);
            }
        }
    }

    public static void resetCrossGroupRank() {
      /*  crossGroupRankMsgMap.clear();
        crossGroupUnionRankMsg = null;*/
        crossGroupMammonMsgMap.clear();
        crossGroupMammonRecordMsgMap.clear();
    }

   /* public static RankProto.RankListMsg.Builder getCrossGroupRankMsg(int type, int bigType) {
        if (null != crossGroupRankMsgMap && crossGroupRankMsgMap.containsKey(bigType) && crossGroupRankMsgMap.get(bigType).containsKey(type)) {
            return crossGroupRankMsgMap.get(bigType).get(type).toBuilder();
        }
        return RankProto.RankListMsg.newBuilder();
    }*/

 /*   public static RankProto.UnionRankListMsg.Builder getCrossGroupUnionRankMsg() {
        if (null != crossGroupUnionRankMsg) {
            return crossGroupUnionRankMsg.toBuilder();
        }
        return RankProto.UnionRankListMsg.newBuilder();
    }*/

    public static DecorationProto.MammonListMsg.Builder getCrossGroupMammonMsg(int decorationId) {
        if (null != crossGroupMammonMsgMap && crossGroupMammonMsgMap.containsKey(decorationId)) {
            return crossGroupMammonMsgMap.get(decorationId).toBuilder();
        }
        return DecorationProto.MammonListMsg.newBuilder();
    }

    public static DecorationProto.MammonRecordListMsg.Builder getCrossGroupMammonRecordMsg(int decorationId) {
        if (null != crossGroupMammonRecordMsgMap && crossGroupMammonRecordMsgMap.containsKey(decorationId)) {
            return crossGroupMammonRecordMsgMap.get(decorationId).toBuilder();
        }
        return DecorationProto.MammonRecordListMsg.newBuilder();
    }

    public static Map<Integer, DecorationProto.MammonListMsg> getCrossGroupMammonMsgMap() {
        return crossGroupMammonMsgMap;
    }

    public static CrossTradeWarProto.CrossTradeWarNoticeListMsg.Builder getCrossTradeWarNoticeListMsg(int activityId,int groupId) {
        if (crossTradeWarNoticeListMsgMap.containsKey(activityId)) {
            if (crossTradeWarNoticeListMsgMap.get(activityId).containsKey(groupId)){
                return crossTradeWarNoticeListMsgMap.get(activityId).get(groupId).toBuilder();
            }else {
                return CrossTradeWarProto.CrossTradeWarNoticeListMsg.newBuilder();
            }
        } else {
            return CrossTradeWarProto.CrossTradeWarNoticeListMsg.newBuilder();
        }
    }
}
